package com.sky.config;

import com.sky.json.JacksonObjectMapper;
import com.sky.json.RedisJacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
 * @ProjectName: sky-take-out
 * @Package: com.sky.config
 * @ClassName: RedisConfiguration
 * @Author: 王加其
 * @Date: 2023/4/20 15:47
 * @Version: 1.0
 */
@Slf4j
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        /*RedisTemplate redisTemplate = new RedisTemplate();
        //1、设置redis的连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);

        //2、设置redis key的序列化器（字符串序列化器）
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());*/

        StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);

        //3、设置redis value的序列化器
        //3.1、新建redis json序列化器
        Jackson2JsonRedisSerializer redisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        redisSerializer.setObjectMapper(new RedisJacksonObjectMapper());


        redisTemplate.setValueSerializer(redisSerializer);
        redisTemplate.setHashValueSerializer(redisSerializer);
        return redisTemplate;
    }
}
